//
//  TOSearchViewController.m
//  X-Tribe
//
//  Created by Miss.Ou on 16/4/4.
//  Copyright © 2016年 Miss.Ou. All rights reserved.
//

#import "TOSearchViewController.h"
#import "TOSearchTableViewCell.h"

#define kISFIRST 120
#define kISNOTFIRST 60


@interface TOSearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITextField *search;

@property (nonatomic, strong) UIButton *cancel;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSString *lightString;

@end

@implementation TOSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE;
    [self createSearchView];
}

-(void)createSearchView{

    //返回按钮
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar_back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
//    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    //搜索栏
    UIView *iconView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation_search_n"]];
    imageView.frame = CGRectMake(5, 5, 20, 20);
    [iconView addSubview:imageView];
    
    self.search = [[UITextField alloc] initWithFrame:CGRectMake(50, 6, SCREEN_WIDTH - kISFIRST, 30)];
    self.search.leftView = iconView;
    self.search.tintColor = [UIColor whiteColor];
    self.search.leftViewMode = UITextFieldViewModeAlways;
    self.search.placeholder = @" 搜索活动 你感兴趣的";
    self.search.delegate = self;
    self.search.textColor = [UIColor whiteColor];
    self.search.backgroundColor = [UIColor grayColor];
    self.search.layer.cornerRadius = 5;
    self.search.layer.masksToBounds = YES;
    self.search.clearsOnBeginEditing = YES;
    self.search.keyboardType = UIKeyboardTypeWebSearch;
    [self.navigationController.navigationBar addSubview:self.search];
    
    [self.search becomeFirstResponder];
    
    self.cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancel.frame = CGRectMake(SCREEN_WIDTH - kISNOTFIRST, 6, 50, 30);
    [self.cancel setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.cancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.cancel];
    /*
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TOSearchTableViewCell class] forCellReuseIdentifier:@"searchTableViewCell"];
    [self.view addSubview:self.tableView];
     */
}

#pragma mark - 取消按钮
- (void)cancelAction:(UIButton *)button {
    
    self.search.text = @"";
    [self.search resignFirstResponder];
    
    CGRect frame =  self.search.frame;
    frame.size.width = SCREEN_WIDTH - kISNOTFIRST;
    self.search.frame = frame;
    
    button.hidden = YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    CGRect frame =  textField.frame;
    frame.size.width = SCREEN_WIDTH - kISFIRST;
    textField.frame = frame;
    
    self.cancel.hidden = NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    self.lightString = textField.text;
    [textField resignFirstResponder];
   
    
    return YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    self.search.hidden = YES;
    self.cancel.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.search.hidden = NO;
    self.cancel.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
